package fr.neyb.bernouilli.common.tools;

import java.lang.reflect.Array;

public class MyArrays {
	private MyArrays() {
	}

	/**
	 * a convertor from T to V
	 * 
	 * @author adeconin
	 * 
	 * @param <V>
	 * @param <T>
	 */
	public static interface Convertor<V, T> {
		V getValue(T o);
	}

	public static <V, T> V[] convertArray(Convertor<V, T> accessor, T[] entry,
			Class<V> c) {
		@SuppressWarnings("unchecked")
		V[] res = (V[]) Array.newInstance(c, entry.length);
		// V[] res = (V[]) new Object[entry.length];
		for (int i = 0; i < entry.length; ++i)
			res[i] = accessor.getValue(entry[i]);
		return res;
	}

	public static String toString(Object[] os, String sep) {
		return toString(os, sep, "", "");
	}

	public static String toString(Object[] os, String sep, String begin,
			String end) {
		StringBuilder sb = new StringBuilder(begin);

		boolean app = false;
		for (Object o : os) {
			if (app) {
				sb.append(sep);
			} else {
				app = true;
			}
			sb.append(o);
		}

		return sb.append(end).toString();
	}

}
